package com.biz.primus.ms.auth.api;

import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.model.sso.account.vo.UserReqVo;
import com.biz.primus.model.sso.account.vo.UserResVo;
import com.biz.primus.model.sso.account.vo.UserVo;
import com.biz.primus.ms.auth.dao.po.User;
import com.biz.primus.ms.auth.service.UserService;
import com.biz.primus.ms.auth.vo.UserToUserVo;
import com.biz.primus.ms.base.vo.SpringPageToSpringPageVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Created by younger on 17/11/7.
 */
@RestController
@RequestMapping("soa/userService")
public class UserApiController {

    private final UserService userService;

    @Autowired
    public UserApiController(UserService userService){
        this.userService = userService;
    }

    @ApiOperation("获取用户列表")
    @PostMapping("listUser")
    public SpringPageVO<UserVo> listUser(@RequestBody UserReqVo userReqVo) {
        Page<User> userPage = userService.listUser(userReqVo);
        return SpringPageToSpringPageVO.of(userPage, new UserToUserVo());
    }

    @ApiOperation("获取用户实体")
    @PostMapping("getUser")
    public UserVo getUser(@RequestParam(name = "id") Long id) {
        UserVo userVo = new UserVo();
        User user = userService.getUser(id);
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @ApiOperation("创建一个用户")
    @PostMapping("createUser")
    public void createUser(@RequestBody UserVo userVo) {
        User user = new User();
        BeanUtils.copyProperties(userVo, user);
        userService.createUser(user);
    }

    @ApiOperation("通过userUnionId找到User对象")
    @GetMapping("getUserResVoById")
    public UserResVo getUserResVoById(@RequestParam(name = "userUnionId") String userUnionId){
        UserResVo userResVo = new UserResVo();
        User user = userService.getUserResVoById(userUnionId);

        if(user != null){
            userResVo.setName(user.getDescription());
            userResVo.setUsername(user.getUserUnionId());
        }
        return userResVo;
    }

}
